<template>
    <div id="dictionary">
        <el-button type="primary" @click="showAdd" >添加数据字典组</el-button>
        <el-divider></el-divider>
        <el-table
                :data="tableData.rows"
                style="width: 100%;margin-bottom: 20px;"
                row-key="id"
                border
                :tree-props="{children: 'children', hasChildren: 'true'}">

            <el-table-column
                    prop="id"
                    sortable
                    label="编号">
            </el-table-column>

            <el-table-column
                    prop="label"
                    label="组名">
            </el-table-column>
            <el-table-column
                    prop="code"
                    label="分组代号">
            </el-table-column>
            <el-table-column
                    prop="sort"
                    sortable
                    label="排序(小到大)">
            </el-table-column>
            <el-table-column
                    prop="description"
                    label="字典描述">
            </el-table-column>
            <el-table-column
                    prop="creator"
                    label="创建者">
            </el-table-column>

            <el-table-column
                    prop="gmtCreated"
                    sortable
                    label="创建时间">
            </el-table-column>

            <el-table-column
                    prop="modifier"
                    label="更新者">
            </el-table-column>

            <el-table-column
                    prop="gmtModified"
                    sortable
                    label="更新时间">
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作">
                <template slot-scope="scope">
                    <el-button @click="showDetail(scope.row)" type="text" size="small">详情</el-button>
                    <el-button @click="showEdit(scope.row)" type="text" size="small">编辑</el-button>
                    <el-button @click="deleteDictionary(scope.$index,scope.row)" type="text" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="total, prev, pager, next,sizes"
                :page-size="tableData.size"
                :current-page="tableData.current"
                @current-change="requestData"
                @size-change="requestData"
                :total="tableData.total">
        </el-pagination>

        <el-dialog :title="title" @close="closeDialog" :visible.sync="dialogEditVisible">
            <el-form :model="form">
                <el-form-item label="id" :label-width="'150px'" v-show="form.id !== null">
                    <el-input v-model="form.id" readonly></el-input>
                </el-form-item>
                <el-form-item label="分组代号(用于查找)" :label-width="'150px'">
                    <el-input v-model="form.code"></el-input>
                </el-form-item>
                <el-form-item label="组名" :label-width="'150px'">
                    <el-input v-model="form.label"></el-input>
                </el-form-item>
                <el-form-item label="排序" :label-width="'150px'">
                    <el-input v-model="form.sort"></el-input>
                </el-form-item>
                <el-form-item label="描述" :label-width="'150px'">
                    <el-input v-model="form.description"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogEditVisible = false">取 消</el-button>
                <el-button type="primary" @click="addOrEdit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "dictionary",
        data(){
            return{
                title: null,
                dialogEditVisible: false,
                form:{
                    id: null,
                    code: null,
                    label:null,
                    sort: null,
                    description: null
                },
                tableData:{
                    size:null,
                    current:null,
                    total:null,
                    rows: [
                        {
                            id: null,
                            code: null,
                            description: null
                        }
                    ]
                }
            }
        },
        methods:{
            async requestData() {
                let resp = await this.$http.post("/sysDictionary/getParentList");
                let data = resp.data;
                if (data.code === 200) {
                    this.tableData = data;
                } else {
                    this.$message.error(data.info);
                }
            },
            deleteDictionary(index,row){
                this.$confirm('确定删除?','提示',{
                    confirmButtonText:'确定',
                    cancelButtonText:'取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete('/sysDictionary/deleteAndChild/'+row.id).then(resp => {
                        let data = resp.data;
                        if (data.code === 200){
                            this.$message.success(data.info);
                            this.requestData();
                        }else {
                            this.$message.success(data.info);
                        }
                    })
                }).catch(() => {})
            },
            showDetail(row){
                this.$router.push({
                    name: "字典详情",
                    query: {
                        code: row.code,
                        label: row.label
                    }
                });
                /*this.$router.push({
                    path: "/sysPermission",
                    query: {
                        id: row.id,
                        roleName: row.roleName
                    }
                })*/

            },
            showEdit(row){
                this.title = '编辑字典';
                this.form = {
                    id: row.id,
                    code: row.code,
                    label:row.label,
                    sort: row.sort,
                    description: row.description
                };
                this.dialogEditVisible = true;
            },
            showAdd(){
                this.title = '新增字典';
                this.form = {
                    id: null,
                    code: null,
                    label:null,
                    sort: null,
                    description: null
                };
                this.dialogEditVisible = true;
            },
            closeDialog(){
                this.form = {
                    id: null,
                    code: null,
                    label:null,
                    sort: null,
                    description: null
                };
            },
            async addOrEdit() {
                let resp;
                if (this.form.id === null) {
                    resp = await this.$http.post("/sysDictionary/add", this.form);
                } else {
                    resp = await this.$http.put("/sysDictionary/edit", this.form);
                }
                let data = resp.data;
                if (data.code === 200) {
                    this.$message.success(data.info);
                    this.requestData();
                    this.dialogEditVisible = false;
                } else {
                    this.$message.error(data.info);
                }
            },
        },
        created() {
            this.requestData();
        }

    }
</script>

<style scoped>

</style>
